import axios from 'axios'
// import Auth from '@/utils/Auth'

const get = (url, cb) => {
  axios.get(url,
    // {
    //   headers: {'Authorization': Auth.getAccessToken()},
    // }
  )
    .then(response => {
      cb(response.data)
    })
    .catch(error => {
      console.log(error)
    })
}

const post = (url, params, cb) => {
  axios.post(url, params,
    // {
    //   headers: {'Authorization': Auth.getAccessToken()},
    // }
  )
    .then(response => {
      cb(response.data)
    })
    .catch(error => {
      console.log(error)
    })
}

// 下载
const downloadUrl = url => {
  let iframe = document.createElement('iframe')
  iframe.style.display = 'none'
  iframe.src = url
  iframe.onload = function () {
    document.body.removeChild(iframe)
  }
  document.body.appendChild(iframe)
}

axios.interceptors.response.use(res => {
  if (res.headers && (res.headers['content-type'] === 'application/octet-stream')) {
    downloadUrl(res.request.responseURL)
  }
})

export default { get, post }
